using System;
using UniversalAdmin.Shared.Enums;

namespace UniversalAdmin.Application.Commands;

/// <summary>
/// API响应结果类
/// </summary>
public class ApiResult
{
    /// <summary>
    /// HTTP状态码
    /// </summary>
    public int Code { get; set; }

    /// <summary>
    /// 操作结果消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 业务数据（可为null）
    /// </summary>
    public object? Data { get; set; }

    /// <summary>
    /// 请求路径（仅错误时返回）
    /// </summary>
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;


  public ApiResult()

    {
 


    // Timestamp 已有默认值，无需再赋值
    }

    /// <summary>
    /// 创建成功响应
    /// </summary>
    /// <param name="message">成功消息</param>
    /// <param name="data">业务数据</param>
    /// <returns>ApiResult实例</returns>
    public static ApiResult Success(string message = "操作成功", object? data = null)
    {
        return new ApiResult
        {
            Code = (int)ApiStatusCode.SUCCESS,
            Message = message,
            Data = data
        };
    }

    public static ApiResult Success(int code, string message, object? data = null)
        => new ApiResult { Code = code, Message = message, Data = data, Timestamp = DateTime.UtcNow };

  /// <summary>
  /// 创建错误响应
  /// </summary>
  /// <param name="code">HTTP状态码</param>
  /// <param name="message">错误消息</param>
  /// <param name="path">请求路径</param>
  /// <param name="details">错误详情</param>
  /// <returns>ApiResult实例</returns>
  public static ApiResult Failed(int code, string message) => new() { Code = code, Message = message };

}


public class ApiResult<T>
{
    public int Code { get; set; } // 使用枚举状态码
    public string Message { get; set; } = "";
    public T? Data { get; set; }
    public bool Succeeded => Code == (int)ApiStatusCode.SUCCESS;

    public DateTime Timestamp { get; set; } = DateTime.UtcNow;

    public static ApiResult<T> Success(T? data = default, string? message = null)
        => new()
        { Code = (int)ApiStatusCode.SUCCESS, Message = message ?? "操作成功", Data = data };

    public static ApiResult<T> Failed(string message, int code = (int)ApiStatusCode.BAD_REQUEST)
        => new()
        { Code = code, Message = message };

    public static ApiResult<T> Failed(int code, string message)
        => new()
        { Code = code, Message = message };

    public static ApiResult<T> Success(int code, string message, T data)
        => new ApiResult<T> { Code = code, Message = message, Data = data, Timestamp = DateTime.UtcNow };
}
